package cn.rongdao.jrkabao.ui.main.mainfragment;


import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;

import com.mifengkong.frtools.app.FRApplication;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRSharedPreferences;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.titlebar.WebViewTitleBarUIComponent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import cn.mifengkong.appcommon.util.ChannelUtil;
import cn.mifengkong.appcommon.webview.OnTabActivityResultListener;
import cn.mifengkong.appcommon.webview.uicomponent.WebViewUIComponent;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseViewPagerFragment;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.ui.webview.WebViewHelper;

/**
 * Created by jack on 17/5/9
 * 其他贷款页
 */

public class FragmentOtherLoan extends BaseViewPagerFragment implements OnTabActivityResultListener {

    @BindView(R.id.fol_webview_uicomponent)
    WebViewUIComponent webViewUIComponent;
    @BindView(R.id.lw_titlebar)
    WebViewTitleBarUIComponent titleBarUIComponent;
    private WebViewHelper webViewHelper;
    private boolean isRefresh = true;
    private String mUrl;

    @Override
    protected int layoutRes() {
        return R.layout.fragment_other_loan;
    }

    @Override
    protected void onViewReallyCreated(View view) {
        super.onViewReallyCreated(view);
        webViewHelper = new WebViewHelper(this, webViewUIComponent, titleBarUIComponent);
        initTitleBar();
        EventBus.getDefault().register(this);

        if (!FRString.isEmpty(mUrl)) {
            webViewHelper.loadUrlAndSetLocalStorage(mUrl);
        }
    }

    @Override
    protected void lazyLoad() {
        webViewUIComponent.requestPermission();
        mUrl = FRSharedPreferences.getStringNotClear(SPConstant.ALL_OTHER_LOAN_URL, "");
        if (FRString.isEmpty(mUrl)) {
            mUrl = FRCollection.appendAsString(InfoConstant.OTHER_LOAN_URL, "?pkname=", BuildConfig.APPLICATION_ID, "&sid="
                    , ChannelUtil.getChannelName(FRApplication.getInstance()), "&type=kamao", "");
        }
        if (null != webViewHelper) {
            webViewHelper.loadUrlAndSetLocalStorage(mUrl);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && !isRefresh) {
            webViewHelper.refreshLoad();
            isRefresh = true;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        webViewHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public boolean onKeyDown(int keyCode) {
        return webViewHelper.onKeyDown(keyCode);
    }

    private void initTitleBar() {
        webViewHelper.initTitleBar(R.drawable.icon_close_white, R.drawable.icon_refresh);
    }

    @Override
    public void onStop() {
        super.onStop();
        webViewHelper.onStop();
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        webViewHelper.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onTabActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != webViewUIComponent) {
            webViewUIComponent.onActivityResult(requestCode, resultCode, data);
            webViewHelper.onActivityResult(requestCode, resultCode, data);
        }
    }
}